//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  Board_iPhone_Register2.m
//  DingDing
//
//  Created by LHL on 14/11/22.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "Board_iPhone_Register2.h"
#import "Board_iPhone_Register3.h"
#pragma mark -

@interface Board_iPhone_Register2()
{
	//<#@private var#>
}
@end

@implementation Board_iPhone_Register2{
    int timeout;
}

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
DEF_OUTLET(BeeUILabel, numHint)
DEF_OUTLET(BeeUIButton, getVerifyCode)
DEF_OUTLET(BeeUITextField, password)
DEF_OUTLET(BeeUITextField, password2)
DEF_OUTLET(BeeUITextField, phoneCode)
DEF_MODEL(sendMessageCode, isendMessageCode)
DEF_MODEL(phoneRegister, iphoneRegister)
- (void)load
{
    self.isendMessageCode = [sendMessageCode modelWithObserver:self];
    self.iphoneRegister = [phoneRegister modelWithObserver:self];
}

- (void)unload
{
}

-(phoneRegister *)iiphoneRegister{
    return self.iphoneRegister;
}
-(sendMessageCode *)iiphoneCodeVerify{
    return self.isendMessageCode;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}


#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.navigationBarShown = YES;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    self.numHint.text = [NSString stringWithFormat:@"验证码已发送至：%@",self.phoneNum];
    [button addTarget:self action:@selector(nextView) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"下一步" forState:0];
    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [button setTitleColor:[UIColor whiteColor] forState:0];
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [v addSubview:button];
    self.title = @"注册";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:v];
    self.navigationBarLeft = [UIImage imageNamed:@"back.png"];
    [self ButtonStatusChange];
}

-(void)ButtonStatusChange{
    self.getVerifyCode.enabled = NO;
    timeout=120; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timers = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timers,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timers, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timers);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getVerifyCode setTitle:@"发送验证码" forState:UIControlStateNormal];
                self.getVerifyCode.userInteractionEnabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getVerifyCode setTitle:[NSString stringWithFormat:@"重新发送(%d)",timeout] forState:0];
            });
            timeout--;
        }
    });
    dispatch_resume(_timers);
}

-(void)passwordIdentify{
    if (self.phoneCode.text.length !=6) {
        [self presentMessageTips:@"请输入6位验证码"];
    }
    if (self.password.text.length<6 || self.password2.text.length < 6) {
        [self presentMessageTips:@"密码长度必须为6~16位"];
        return;
    }
    if (![self.password.text isEqualToString:self.password2.text]) {
        [self presentMessageTips:@"密码与重复密码不一致"];
        return;
    }
    
}

ON_SIGNAL3(BeeUIButton, getVerifyCode, SIGNAL){
    [self iiphoneCodeVerify].phone = self.phoneNum;
    [[self iiphoneCodeVerify]firstPage];
}

ON_SIGNAL3(phoneCodeVerify, RELOADED, SIGNAL){
    [self dismissTips];
    [self ButtonStatusChange];
}

ON_SIGNAL3(phoneCodeVerify, RELOADING, SIGNAL){
    [self presentLoadingTips:@"加载中"];
}

ON_SIGNAL3(phoneRegister, RELOADED, SIGNAL){
    [self dismissTips];
    if ([[self iiphoneRegister].code isEqualToString:@"200"]) {
        Board_iPhone_Register3 *res = [[Board_iPhone_Register3 alloc]init];
        [self.navigationController pushViewController:res animated:YES];
    }else{
        if ([[self iiphoneRegister].code isEqualToString:@""]|| [self iiphoneRegister].code ==nil) {
            [self presentMessageTips:@"网络链接错误"];
        }else{
            [self presentMessageTips:[self iiphoneRegister].message];
        }
    }
}

ON_SIGNAL3(phoneRegister, RELOADING, SIGNAL){
    [self presentLoadingTips:@"加载中"];
}


ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)nextView{
    [self resign];
    [self iiphoneRegister].phone = self.phoneNum;
    [self iiphoneRegister].verifyCode = self.phoneCode.text;
    [self iiphoneRegister].password = self.password.text;
    [[self iiphoneRegister]firstPage];

}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
 
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self resign];
}

-(void)resign{
    for (UIView *t in self.view.subviews) {
        if ([t isKindOfClass:[BeeUITextField class]]) {
            [t resignFirstResponder];
        }
    }
}

@end
